import { RouteRecordRaw, createRouter, createWebHistory } from "vue-router";
import { ElMessage } from "element-plus";
import route from "@/utils/yzy/route";
const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    component: () => import("@/views/login.vue"),
  },
  {
    path: "/login",
    component: () => import("@/views/login.vue"),
  },
  {
    path: "/home",
    component: () => import("@/views/home.vue"),
  },
];
const router = createRouter({
  routes,
  history: createWebHistory(),
});

router.beforeEach(async (to, from, next) => {
  if (to.path == "/login") {
    next();
  } else {
    const token = localStorage.getItem("token");
    if (!token) {
      ElMessage.warning("用户尚未登录，请登录");
      next("/login");
    } else {
      route();
      next();
    }
  }
});
export default router;
